Phong Shading

This tutorial shows how to do some very basic (and slow) Phong Shading. Source code is provided in AMOS, under the PhongShading.AMOS file in the Mag5/ drawer. I won't, however, be covering the source code closely, rather, I will be covering the theory of it here. A knowledge of vectors is useful, but if you don't understand it, you can still fiddle with the source code.

Phong Shading is a simple method of applying lighting effects to 3D objects. The algorithm takes into account a single point light source, but no reflections of light from other objects. The light at a given point on the object is calculated as a combination of three parts:

Diffuse is given by: Diffuse = I * KD * cos a
where I is the light intensity, KD is a coefficient (constant), and a is the angle between the surface normal and the light direction - normal means a line perpendicular to the surface. In vector notation, this becomes: Diffuse = I * KD * (L . N)
where L is a unit vector in the direction of the light beam, and N is a unit surface normal.

Ambient is easy, it's always a constant value: Ambient = IAKA

Specular is given by: Specular = I * KS * (cos b)^n
where KS is another constant coefficient. b is the angle between the viewing direction and the surface normal. In vector notation, with R equal to a unit vector in the viewing direction, we have: Diffuse = I * KS * (R . V)^n
n is how rough the surface is; a perfect mirror would have n=infinity. For the specular is a measure of how much light is reflected towards the viewpoint.

Adding these three gives a brightness value, which can be used to determine the colour (not so easy when we aren't working in 24 bit!)

The two programs are more or less the same; they both draw a 3D sphere, with a fixed viewpoint.

Mark